/**
 * Created by Administrator on 15-8-3.
 */
//图片上传
var ImgUploador = (function(){


	function init(){
		var uploadImgUl = $("#uploadImgUl");
		var initPath = $("#imgpathGrpHidInp").val().split(",");
		var html = "";
		for(var i in initPath){
			var path = initPath[i];
			if(path){
				html += parseTemplate({src:path})
			}
		}
		uploadImgUl.html(html);
		uploadImgUl.on("click",".removeBtn",function(e){
			var tarBtn = $(e.currentTarget);
			var result = confirm("确定删除该图吗");
			if(!result) return false;
			tarBtn.parents(".item").remove();
		})
		$("#browerFileBtn").on("change",function(e){
			$("#file_name_inp").val(e.target.value || "");
		})
	}
	function parseTemplate(data){
		var src = data.src;
		if(!src) return false;
		return '<li class="item"><table><tbody><tr><td><input type="hidden" value="'+src+'" name="thumb_img[]"/><input name="imgpathGrp[]" value="'+src+'" type="hidden"/><img class="imgPhoto" style="width:100%" src="'+src+'" alt=""/></td></tr></tbody></table><span class="removeBtn"><i class="iconfont">&#xe68f;</i></span></li>';
	}
	function fixImg(img){
//		var wrap = $("#uploadImgUl");
//		console.log("onload")
//		wrap.find(".imgPhoto").each(function(){
//			var tarImg = $(this);
//			var imgObj = tarImg[0];
//			console.log(tarImg.width());
//			console.log(imgObj);
//			console.log(imgObj.offsetWidth);
//			console.log(imgObj.scrollWidth);
//		})
	}
	//图片上传完成
	function complete(data){
		if(data.status=="ok" && data.src){
			var html = parseTemplate({src:data.src})
			$("#uploadImgUl").append(html);
			Alert("success",'<p style="width:160px;">图片上传成功</p>');
		}else{
			Alert("fail",'<p style="width:160px;">图片上传失败</p>');
		}
		$("#file_name_inp").val("");
	}

	return{
		init : init,
		complete : complete,
		parseTemplate : parseTemplate,
		fixImg : fixImg
	}

})();